import re

def clean_line(line):
    try:
        # 提取数据内容（去除开头的 `b` 和两侧的 `[]`）
        match = re.search(r'\[(.*)\]', line)
        if not match:
            raise ValueError("无法解析行数据")
        
        # 将数据拆分，并去除每个元素的 `b'` 或 `b"` 前缀
        raw_items = match.group(1).split(', ')
        cleaned = [item.strip("b'\" ") for item in raw_items]
        
        # 如果数据长度不够，填充空字符串
        while len(cleaned) < 21:
            cleaned.append('')
        
        # 组合成符合要求的格式
        formatted_line = ','.join(cleaned)
        
        return formatted_line + '\n'
    except Exception as e:
        print(f"处理行出错: {e}")
        return None  # 返回 None 以便在主函数中过滤掉

def process_file(input_file_path, output_file_path):
    with open(input_file_path, 'r', encoding='utf-8') as infile, open(output_file_path, 'w', encoding='utf-8') as outfile:
        for line in infile:
            cleaned_line = clean_line(line)
            if cleaned_line:
                outfile.write(cleaned_line)

# 使用函数，只需提供输入文件路径和输出文件路径
input_file_path = r'D:\files\农业机器人任务规划研究\ros_workspace\navigation2_tutorials\my_test_dir\gps\all_process\new_gps.txt'
output_file_path = r'D:\files\农业机器人任务规划研究\ros_workspace\navigation2_tutorials\my_test_dir\gps\all_process\new_gps_process.txt'

# 处理文件
process_file(input_file_path, output_file_path)